<template>
  <!-- 导入文件页面 -->
  <UploadExcel :on-success="onSuccess" />
</template>

<script>
import { importEmployee } from '@/api/employees'
export default {
  methods: {
    // 导入成功
    async onSuccess(data) {
      console.log(data)
      const arr = data.results.map(item => {
        return this.zh2en(item)
      })
      console.log(arr)
      //   发请求
      await importEmployee(arr)
      // 提示
      this.$message.success('上传成功')
      // 返回上一页
      this.$router.back()
    },
    // 改造数组数据 中专英
    zh2en(item) {
      // 字典
      const dict = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber'
      }
      const newObj = {}
      for (const key in item) {
        // console.log(key, item[key])  mobile,13041139879
        const newKey = dict[key] // mobile
        const newVal = item[key] //  13041139879
        if (newKey === 'correctionTime' || newKey === 'timeOfEntry') {
          newObj[newKey] = this.excelFormatDate(newVal)
        } else {
          newObj[newKey] = newVal
        }
      }
      return newObj
    },
    // Excel上传后时间格式化
    excelFormatDate(numb, format = '-') {
      const time = new Date((numb - 1) * 24 * 3600000 + 1)
      time.setYear(time.getFullYear() - 70)
      const year = time.getFullYear() + ''
      const month = time.getMonth() + 1 + ''
      const date = time.getDate() + ''
      if (format && format.length === 1) {
        return year + format + month + format + date
      }
      return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
    }

  }
}
</script>

<style>

</style>
